﻿// HSS.Interlink.Converters.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.html)
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       Converters.cs
// Author:     HSS\gbanta
// Created:    07/16/2011
// Modified:   07/22/2011
// ----------------------------------------------------------------------------
namespace HSS.Interlink
{
	#region Using Directives
	using System;
	using System.Windows.Data;
	#endregion

	#region ByteConverter
	/// <summary>
	/// ByteConverter converts byte (double) values, in to a friendly such as 10KB or 500 bytes
	/// </summary>
	public class ByteConverter : IValueConverter
	{
		#region IValueConverter Members
		/// <summary>
		/// Convert byte values to String Representation as byte, kb or gb
		/// </summary>
		/// <param name="value">The value to convert</param>
		/// <param name="targetType">The destination type</param>
		/// <param name="parameter">Optional and unused parameter</param>
		/// <param name="culture">Curernt Thread Culture</param>
		/// <returns>A string contains the size and size indicator as a string</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			string size = "0 KB";
			if (value != null)
			{
				bool round = false;

				if (null != parameter && parameter is string)
					round = bool.Parse((string)parameter);

				double fileSize = System.Convert.ToDouble(value);

				if (fileSize < 1024.0)
				{
					if (round)
						return "1 KB";
					else
						return string.Format("{0:N0} bytes", fileSize);
				}

				if (fileSize < 1048576.0)
				{
					if (round)
						return string.Format("{0:N0} KB", Math.Ceiling(fileSize / 1024.0));
					else
						return string.Format("{0:N2} KB", fileSize / 1024.0);
				}

				if (fileSize < 1073741824.0)
				{
					if (round)
						return string.Format("{0:N0} MB", Math.Ceiling(fileSize / 1048576.0));
					else
						return string.Format("{0:N2} MB", fileSize / 1048576.0);
				}

				return string.Format("{0:N2} GB", fileSize / 1073741824.0);
			}
			return size;
		}
		/// <summary>
		/// We only use one-way binding, so we don't implement this.
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}
		#endregion

		internal string CovertToString(double value)
		{
			if (value < 1024.0)
				return string.Format("{0:N0} bytes", value);

			if (value < 1048576.0)
				return string.Format("{0:N2} KB", value / 1024.0);

			if (value < 1073741824.0)
				return string.Format("{0:N2} MB", value / 1048576.0);

			return string.Format("{0:N2} GB", value / 1073741824.0);
		}
	}
	#endregion

	#region PercentageConverter
	/// <summary>
	/// PercentageConverter converts a percentage value, in to a friendly such as 10% or 100%
	/// </summary>
	public class PercentageConverter : IValueConverter
	{
		#region IValueConverter Members
		/// <summary>
		/// Convert byte values to String Representation as byte, kb or gb
		/// </summary>
		/// <param name="value">The value to convert</param>
		/// <param name="targetType">The destination type</param>
		/// <param name="parameter">Optional and unused parameter</param>
		/// <param name="culture">Curernt Thread Culture</param>
		/// <returns>A string contains the size and size indicator as a string</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			string size = "{0} %";
			if (value != null)
				return string.Format(size, value);
			return string.Format(size, 0);
		}
		/// <summary>
		/// We only use one-way binding, so we don't implement this.
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}
		#endregion

		internal string CovertToString(double value)
		{
			return string.Format("{0} %", value);
		}
	}
	#endregion

	#region TransferRateConverter
	/// <summary>
	/// TransferRateConverter converts a TransferRate value, in to a friendly such as 2KB/s or 1MB/s
	/// </summary>
	public class TransferRateConverter : IValueConverter
	{
		#region IValueConverter Members
		/// <summary>
		/// Convert byte values to String Representation
		/// </summary>
		/// <param name="value">The value to convert</param>
		/// <param name="targetType">The destination type</param>
		/// <param name="parameter">Optional and unused parameter</param>
		/// <param name="culture">Curernt Thread Culture</param>
		/// <returns>A string contains the size and size indicator as a string</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			string size = "0 KB/s";
			if (value != null)
			{
				double fileSize = (double)value;

				if (fileSize < 1024.0)
					return string.Format("{0:N0} bytes/s", fileSize);

				if (fileSize < 1048576.0)
					return string.Format("{0:N0} KB/s", fileSize / 1024.0);

				if (fileSize < 1073741824.0)
					return string.Format("{0:N2} MB/s", fileSize / 1048576.0);

				return string.Format("{0:N2} GB/s", fileSize / 1073741824.0);
			}
			return size;
		}
		/// <summary>
		/// We only use one-way binding, so we don't implement this.
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}

		internal string ConvertToString(double bytes)
		{
			if (bytes < 1024.0)
				return string.Format("{0:N0} bytes/s", bytes);

			if (bytes < 1048576.0)
				return string.Format("{0:N0} KB/s", bytes / 1024.0);

			if (bytes < 1073741824.0)
				return string.Format("{0:N2} MB/s", bytes / 1048576.0);

			return string.Format("{0:N2} GB/s", bytes / 1073741824.0);

		}
		#endregion
	}
	#endregion

	#region TimeConverter
	/// <summary>
	/// Converts a TimeSpan or Double to a Time String
	/// </summary>
	public class TimeConverter : IValueConverter
	{
		#region IValueConverter Members
		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (null != value)
			{
				TimeSpan ts;

				if (value is double)
					ts = TimeSpan.FromSeconds((double)value);
				else if (value is TimeSpan)
					ts = (TimeSpan)value;
				else
					return "0 sec";

				if (ts.Hours > 0)
					return string.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds);

				else if (ts.Minutes > 0)
					return string.Format("{0:00}:{1:00}", ts.Minutes, ts.Seconds);

				else
					return string.Format("00:00:{0:00}", ts.TotalSeconds);
			}
			return "0 sec";
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}

		internal string CovertToString(double value)
		{
			TimeSpan ts = TimeSpan.FromSeconds(value);
			if (ts.Hours > 0)
				return string.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds);
			else if (ts.Minutes > 0)
				return string.Format("{0:00}:{1:00}", ts.Minutes, ts.Seconds);
			else
				return string.Format("00:00:{0:00}", ts.TotalSeconds);
		}
		#endregion
	}
	#endregion

	#region FullTimeConverter
	/// <summary>
	/// Converts a TimeSpan or Double to a Friendly Time String
	/// </summary>
	public class FullTimeConverter : IValueConverter
	{
		#region IValueConverter Members
		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (null != value)
			{
				TimeSpan ts;

				if (value is double)
					ts = TimeSpan.FromSeconds((double)value);
				else if (value is TimeSpan)
					ts = (TimeSpan)value;
				else
					return "0 sec";

				if (ts.Hours > 0)
					return string.Format("{0:00} hrs  {1:00} min  {2:00} sec", ts.Hours, ts.Minutes, ts.Seconds);

				else if (ts.Minutes > 0)
					return string.Format("{0:00} min  {1:00} sec", ts.Minutes, ts.Seconds);

				else if (ts.Seconds > 0)
					return string.Format("{0:00} min  {1:00} sec  {2:00} ms", ts.Minutes, ts.Seconds, ts.Milliseconds);

				else
					return string.Format("{0:00} sec  {1:00} ms", ts.Seconds, ts.Milliseconds);

			}
			return "0 sec";
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}

		#endregion
	}
	#endregion
}